package com.liit.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.liit.entity.DictFormMap;
import com.liit.entity.PartyFormMap;
import com.liit.entity.RegionFormMap;
import com.liit.mapper.DictMapper;
import com.liit.mapper.PartyMapper;
import com.liit.mapper.RegionMapper;
import com.liit.util.Common;
import com.liit.util.EhcacheUtils;
import com.liit.util.RegionUtils;

/**
 * 
 * @author dingweibo
 * @Title: SpringTaskController.java
 * @Description: Spring调度，指定时间执行 利用了spring中使用注解的方式来进行任务调度。
 * @date 2016年3月27日
 */
@Component
public class SpringTaskController {

	private static final Logger logger = LoggerFactory.getLogger(SpringTaskController.class);

	@Resource
	private RegionMapper regionMapper;

	@Resource
	private DictMapper dictMapper;

	@Resource
	private PartyMapper partyMapper;

	/**
	 * 加载系统省市区配置
	 * 
	 * @throws Exception
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 60 * 24)
	public void loadRegionTask() throws Exception {
		long now = System.currentTimeMillis();
		logger.info("定时任务执行:加载省市区信息.............");

		RegionFormMap where = new RegionFormMap();
		where.put("orderby", " order by parentid,sort");
		List<RegionFormMap> result = regionMapper.findByNames(where);

		RegionUtils.build(result);

		logger.info("加载省市区信息:" + result.size());
		logger.info("加载省市区信息完成,耗时:" + (System.currentTimeMillis() - now));
	}

	/**
	 * 更改活动状态
	 * 
	 * @throws Exception
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 5)
	public void editPlayStatus() throws Exception {
		long now = System.currentTimeMillis();
		logger.info("定时任务执行:更改活动状态.............");
		PartyFormMap party = new PartyFormMap();
		party.put("where", "status=" + Common.PARTY_STATUS_PASS + " and startTime < '" + Common.fromDateH() + "' and endTime > '" + Common.fromDateH() + "'");
		List<PartyFormMap> partlist = partyMapper.findByWhere(party);
		Integer id = null;
		for (PartyFormMap partyFormMap : partlist) {
			id = partyFormMap.getInt("id");
			partyFormMap.clear();
			partyFormMap.put("id", id);
			partyFormMap.put("status", Common.PARTY_STATUS_BEGIN);
			partyMapper.editEntity(partyFormMap);
		}
		party.put("where", "status in (" + Common.PARTY_STATUS_PASS + "," + Common.PARTY_STATUS_BEGIN + ") and endTime < '" + Common.fromDateH() + "' ");
		List<PartyFormMap> ptList = partyMapper.findByWhere(party);
		for (PartyFormMap partyFormMap : ptList) {
			id = partyFormMap.getInt("id");
			partyFormMap.clear();
			partyFormMap.put("id", id);
			partyFormMap.put("status", Common.PARTY_STATUS_FINISH);
			partyMapper.editEntity(partyFormMap);
		}
		logger.info("更改活动状态完成,耗时:" + (System.currentTimeMillis() - now));
	}

	/**
	 * 加载系统字典
	 * 
	 * @throws Exception
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 30)
	public void loadDictTask() throws Exception {
		long now = System.currentTimeMillis();
		logger.info("定时任务执行:加载系统字典信息.............");

		DictFormMap where = new DictFormMap();
		where.put("orderby", " order by type,sort");
		List<DictFormMap> result = dictMapper.findByNames(where);

		Map<Object, Object> dictMap = (Map<Object, Object>) EhcacheUtils.get("dict");
		if (dictMap == null) {
			dictMap = new HashMap<Object, Object>();
		} else {
			dictMap.clear();
		}
		for (DictFormMap record : result) {
			dictMap.put(record.getInt("id"), record);
			String type = record.getStr("type");
			List<DictFormMap> group = (List<DictFormMap>) dictMap.get(type);
			if (group == null) {
				group = new ArrayList<DictFormMap>();
				dictMap.put(type, group);
			}
			group.add(record);
		}
		EhcacheUtils.put("dict", dictMap);

		logger.info("加载字典信息:" + result.size());
		logger.info("加载系统字典信息完成,耗时:" + (System.currentTimeMillis() - now));
	}
}